package com.weixing.mall.provider.controller;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.enums.IsReadEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.model.domain.MemberMessage;
import com.weixing.mall.provider.model.dto.MemberMessageDto;
import com.weixing.mall.provider.model.enums.MemberMsgTypeEnum;
import com.weixing.mall.provider.service.IMemberMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MemberMessageController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/9/17
 * @Version 1.0
 **/

@RestController
@RequestMapping("memberMessage")
@Api(value = "會員消息Api",tags = "会员消息")
public class MemberMessageController extends BaseController {

    @Autowired
    private IMemberMessageService memberMessageService;

    @ApiOperation(value = "查询详情")
    @GetMapping("/{id}")
    public Result selectDetail(@PathVariable Long id){
        MemberMessage message=new MemberMessage();
        message.setIsRead(IsReadEnum.ENABLE.getValue());
        message.setId(id);
        memberMessageService.updateById(message);
        return ResultUtil.success(memberMessageService.selectDetail(id));
    }

    @ApiOperation("查询每个用户的消息")
    @PostMapping("member")
    public Result selectByMemberId(@MemInfo MemAuth auth){
        return ResultUtil.success(memberMessageService.selectByMemberId( initMpPage(), auth.getMid()));
    }
    @ApiOperation("查询未阅读的信息条数")
    @GetMapping("noread")
    public Result selectNoReadMessage(@MemInfo MemAuth auth){
        return ResultUtil.success(memberMessageService.noReadMessage(auth.getMid()));
    }

    @ApiOperation("按照类型查询未阅读的信息条数")
    @GetMapping("noReadByType")
    public Result selectnoReadByType(@MemInfo MemAuth auth){
        return ResultUtil.success(memberMessageService.noReadMessageByType(auth.getMid()));
    }
    @ApiOperation("修改状态")
    @PostMapping("updateIsRead/{id}")
    public Result updateIsRead(@PathVariable long id){
        MemberMessage byId = memberMessageService.getById(id);
        byId.setIsRead(IsReadEnum.ENABLE.getValue());
        memberMessageService.updateById(byId);
        return ResultUtil.success();
    }

    @ApiOperation("查询")
    @GetMapping("")
    public Result selectList(){
        return ResultUtil.success(memberMessageService.list());
    }

    @ApiOperation("根据类型查询列表")
    @GetMapping("selectByType/{type}")
    public Result selectListByType(@MemInfo MemAuth auth,@PathVariable  Long type ){
        return ResultUtil.success(memberMessageService.selectMessageByType(type,auth.getMid()));
    }

    @ApiOperation("")
    @PostMapping("save")
    public Result save(@Validated MemberMessageDto memberMessageDto){
        memberMessageService.saveBit(memberMessageDto);
        return ResultUtil.success();
    }

    @ApiOperation("消息回复")
    @PostMapping("msgReply")
    public Result msgReply(@RequestBody MemberMessageDto memberMessageDto){
        memberMessageService.msgReply(memberMessageDto);
        return ResultUtil.success();
    }

    @ApiOperation("根据类型查询列表")
    @GetMapping("selectMemberMsgList")
    public Result selectMemberMsgList(@MemInfo MemAuth auth){
        Integer type = MemberMsgTypeEnum.TYPE1.getValue();
        if(auth.getAbid() != null){
            type = MemberMsgTypeEnum.TYPE2.getValue();
        }
        return ResultUtil.success(memberMessageService.selectMemberMsgList(initMPage(),type));
    }
}
